﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using System.Web.Mvc;
using WaterRevenueMS.WebUI.Common;
using System.ComponentModel.Composition;
using WaterRevenueMS.Core.Service.Maintenance.Impl;
using WaterRevenueMS.WebUI.Extension.Filters;
using WaterRevenueMS.Domain.Common.Models;
using WaterRevenueMS.Domain.ViewModels.Maintenance.RepairRecord;
using WaterRevenueMS.Core.Service.Maintenance;
using WaterRevenueMS.Componnent.Common;
using WaterRevenueMS.Componnent.Entities.Maintenance;
using WaterRevenueMS.WebUI.Extension.MvcHelper;
using WaterRevenueMS.Domain.ViewModels.BasicSet.WaterCaliber;
using WaterRevenueMS.Domain.ViewModels.BasicSet.Manufacturer;
using WaterRevenueMS.Core.Service.BasicSet;

namespace WaterRevenueMS.WebUI.Controllers
{

    [Export]
    [PartCreationPolicy(CreationPolicy.NonShared)]
    public class RepairRecordController : AdminController
    {
        [Import]
        public IRepairRecordService RepairRecordService { get; set; }
        [Import]
        private IWaterCaliberService WaterCaliberService { get; set; }

        [Import]
        private IManufacturerService ManufacturerService { get; set; }


        [AdminLayoutAttribute]
        public ActionResult Index()
        {
            var modell = new RepairRecordModel();
            return View(modell);
        }
        [AdminPermission(PermissionCustomMode.Ignore)]
        public ActionResult List(DataTableParameter param)
        {
            int total = RepairRecordService.RepairRecords.Count(t => t.Chu_LZT == false);

            var expr = BuildSearchCriteria();

            var filterResult = RepairRecordService.RepairRecords.Where(expr).Select(t => new RepairRecordModel
            {
                Id = t.Id,
                Bei_Z = t.Bei_Z,
                Liu_SH = t.Liu_SH,
                Cao_ZY = t.Cao_ZY,
                UserName = t.MeterInfo.Customer != null ? t.MeterInfo.Customer.Hu_ZXM : "",
                Yi_BDZ = t.MeterInfo != null ? t.MeterInfo.Yi_BDZ : "",
                

            }).OrderBy(t => t.Id).Skip(param.iDisplayStart).Take(param.iDisplayLength).ToList();

            int sortId = param.iDisplayStart + 1;

            var result = from c in filterResult
                         select new[]
                        {
                            sortId++.ToString(), 
                            c.UserName,
                            c.Liu_SH,  
                            c.Yi_BDZ,
                            c.Bei_Z,
                            c.Id.ToString()
                        };

            return Json(new
            {
                sEcho = param.sEcho,
                iDisplayStart = param.iDisplayStart,
                iTotalRecords = total,
                iTotalDisplayRecords = total,
                aaData = result
            }, JsonRequestBehavior.AllowGet);
        }



        public ActionResult Create()
        {
            var model = new RepairRecordModel();
           
            return PartialView(model);
        }

        [HttpPost]
        [AdminOperateLog]
        public ActionResult Create(RepairRecordModel model)
        {
            if (ModelState.IsValid)
            {
                OperationResult result = RepairRecordService.Insert(model);
                if (result.ResultType == OperationResultType.Success)
                {
                    return Json(result);
                }
                else
                {
                    return PartialView(model);
                }
            }
            else
            {
                return PartialView(model);
            }
        }

        public ActionResult Edit(int Id)
        {
            var model = new RepairRecordModel();
            var entity = RepairRecordService.RepairRecords.FirstOrDefault(t => t.Id == Id);
            if (null != entity)
            {
                model = new RepairRecordModel
                {
                    Bei_Z =entity.Bei_Z,
                    Id = entity.Id
                    
                };

                model.Jian_XJG = entity.Jian_XYY;

                model.Meter_No = (int)entity.Meter_No;
               
            }
            return PartialView(model);
        }
       [HttpPost]
        public ActionResult Edit(RepairRecordModel model)
        {
            if (ModelState.IsValid)
            {         
                OperationResult result = RepairRecordService.Update(model);
                if (result.ResultType == OperationResultType.Success)
                {
                    return Json(result);
                }
                else
                {
                    return PartialView(model);
                }


            }
            else
            {
                return PartialView(model);
            }
        }

     

        private Expression<Func<RepairRecord, Boolean>> BuildSearchCriteria()
        {
            DynamicLambda<RepairRecord> bulider = new DynamicLambda<RepairRecord>();
            Expression<Func<RepairRecord, Boolean>> expr = null;
            if (!string.IsNullOrEmpty(Request["Liu_SH"]))
            {
                var data = Request["Liu_SH"].Trim();
                Expression<Func<RepairRecord, Boolean>> tmp = t => t.Liu_SH.Contains(data);
                expr = bulider.BuildQueryAnd(expr, tmp);
            }

            if ((!string.IsNullOrEmpty(Request["Chu_LZT"])) && (Request["Cao_ZLX"] != "abcd"))
            {
                var data = Request["Chu_LZT"].Trim();
                Expression<Func<RepairRecord, Boolean>> tmp = t => t.Chu_LZT.ToString().Contains(data);
                expr = bulider.BuildQueryAnd(expr, tmp);
            }
            Expression<Func<RepairRecord, Boolean>> tmpSolid = t => t.Chu_LZT == false;
            expr = bulider.BuildQueryAnd(expr, tmpSolid);

            return expr;
        }
        private void InitModuleSet(RepairRecordModel model)
        {
            //水表口径
            var WatercaliberDatas = WaterCaliberService.WaterCalibers.Where(t => t.IsDeleted == false)
                .Select(t => new WaterCaliberModel
                {
                    Id = t.Id,
                    Shui_BKJ = t.Shui_BKJ
                });
        }

    }

}
